/**
 * Copyright(c): 2018 com.mjduan All rights reserved.
 * 项目名：sofarpcboot-learn
 * 注意：未经作者允许，不得外传
 */
package com.mjudan.project.provider;

import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.mjudan.project.AddressFacade;
import com.mjudan.project.User;
import com.mjudan.project.UserFacade;

import org.springframework.stereotype.Service;

/**
 * @author dmj1161859184@126.com 2021-10-17 17:50
 * @version 1.0
 * @since 1.0
 */
@SofaService(interfaceType = UserFacade.class, bindings = { @SofaServiceBinding(bindingType = "bolt") })
@Service
public class UserServiceImpl implements UserFacade {


    @SofaReference(interfaceType = AddressFacade.class, binding = @SofaReferenceBinding(bindingType = "bolt"))
    private AddressFacade addressFacade;

    @Override
    public User create(String name, String address) {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();

        SofaTracerSpan serverSpan = sofaTraceContext.pop();
        SofaTracerSpanContext sofaTracerSpanContext = serverSpan.getSofaTracerSpanContext();
        String traceId = sofaTracerSpanContext.getTraceId();
        String parentId = sofaTracerSpanContext.getParentId();
        SofaTracer sofaTracer = serverSpan.getSofaTracer();
        System.out.println("user consumer,traceId:{}"+traceId+",parentId:"+parentId);
        User user = new User();
        user.setName(name);
        String address1 = addressFacade.retrieveAddress();
        user.setAddress(address+":"+address1);
        user.setId("1");
        return user;
    }
}
